#!/usr/bin/env python
# encoding: utf-8
"""
dbo.py

Created by Bud Gibson on 2010-07-07.
Copyright (c) 2010 Bud Gibson. Available under the GPL/Artistic License.
"""

from ZODB import FileStorage, DB
import transaction
from BTrees.OOBTree import OOBTree
try:
  from buzztracker.conf import ZODB_FILE
except ImportError:
  import sys
  import os
  sys.path.append(os.path.join(os.path.dirname(__file__), '../..'))
  from buzztracker.conf import ZODB_FILE

class DataBase(object):
  """Creates or opens a ZODB database repository"""
  def __init__(self, path=ZODB_FILE):
    super(DataBase, self).__init__()
    self.storage = FileStorage.FileStorage(path)
    self.db = DB(self.storage)
    self.connection = self.db.open()
    self.dbroot = self.connection.root()

  def get_btree(self,bkey='rundb'):
    """Returns an OOBTree database object under the given key name"""
    if not self.dbroot.has_key(bkey):
      self.dbroot[bkey] = OOBTree()
      transaction.commit()
    return self.dbroot[bkey]

  def close(self):
    self.connection.close()
    self.db.close()
    self.storage.close()
    
  def pack(self):
    """packs the database"""
    self.db.pack()

def main():
  pass


if __name__ == '__main__':
  main()

